<?php
	class category_product_1 extends MY_Controller{	
		function category_product_1() {
			parent::__construct();
			@session_start();
			$this->mod_ad->is_logged_in();		
			$_SESSION['redirect'] = $this->uri->uri_string();
			//kiem tra quyen truy cap 
			$this->check_permission();		
		}
		
		function index(){
			$data['vl_sort'] = "DESC";	
			$data['title'] = "Quản lý danh mục sản phẩm cấp 1";
			$data['table'] = 'category_product';
			$data['controller'] = 'category_product_1';
			$data['content_admin'] = 'back_end/category_product_1/index';
			$data['sort'] = 'ASC';
			
			$this->db->order_by('parent_id','asc');
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai	
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
			
			$per_page = 20;
			$uri = 4;
			$data['admin_category_product_1'] = $this->mod_lib->load_all('category_product','',array('level' => 1),$per_page,$this->uri->segment($uri),array("order"=>"asc"));																													
			$config['base_url']     =  site_url('back_end/admin_menu/index/');
			$this->db->where('level',1);
			$config['total_rows']   =  $this->db->count_all_results('category_product');
			$config['per_page']     =  $per_page; 
			$config['uri_segment']  =  $uri; 
		
			$this->pagination->initialize($config);
			$this->load->view('back_end/template_backend',$data);
		} 
		
		function load (){
			$value_all = $this->input->POST('value_all');
			
			if (!empty($value_all)) {
				foreach ($value_all as $value_all_vl_arr) {
					if(Is_Numeric($value_all_vl_arr)){
						//lay ra order cua ban ghi bi xoa
						$this->db->select('order');
						$this->db->where('level',1);
						$admin_menu_one = $this->db->get_where('category_product',array('id' => $value_all_vl_arr))->row_array();
						$order_one = $admin_menu_one['order'];
						
						//xoa admin menu
						$this->db->delete('category_product', array('id' => $value_all_vl_arr));
						
						//lay ra cac ban ghi co gia tri order lon hon gia tri order cua ban ghi da xoa
						$this->db->select('id,order');
						$this->db->where('level',1);
						$this->db->where("order > $order_one");
						$this->db->order_by("order", "asc");
						$category_product_arr = $this->db->get('category_product')->result_array();
						
						//cong them moi gia order 1 don vi
						foreach ($category_product_arr as $category_product_arr_value) {
							$where = array(
						  			'id' => $category_product_arr_value['id']
						  		);		  		
						  	$order = array();
							$data_value = array (
								'order'       => $category_product_arr_value['order'] - 1
							);
							
							$this->mod_lib->update('category_product',$where,$order,$data_value);
						}
						
						//xoa cac san pham di kem
						$id_category = array (
							'category_product_id' => $value_all_vl_arr
						);
						$this->db->delete('product',$id_category);
						
						$data['success_vl'] = 'Xóa thành công !'; 
						//$this->session->set_flashdata('msview', 'Xóa sản phẩm thành công !');
					}		
				}
			}
			
			$data['vl_sort'] = "DESC";	
			$data['title'] = "Quản lý danh mục sản phẩm cấp 1";
			$data['table'] = 'category_product';
			$data['controller'] = 'category_product_1';
			$data['sort'] = 'ASC';
			$data['content_admin'] = 'back_end/category_product_1/load';
			$per_page = 20;
			$uri = 4;
			$data['admin_category_product_1'] = $this->mod_lib->load_all('category_product','',array('level' => 1),$per_page,$this->uri->segment($uri),array("order"=>"asc"));																													
			$config['base_url']     =  site_url('back_end/category_product_1/index/');
			$this->db->where('level',1);
			$config['total_rows']   =  $this->db->count_all_results('category_product');
			$config['per_page']     =  $per_page; 
			$config['uri_segment']  =  $uri; 
			$this->pagination->initialize($config);
			
			$this->load->view('back_end/category_product_1/load',$data);
		}
		
		function search () {
			$data['vl_sort'] = "DESC";	
			$check_search = $this->input->POST('formSearch');
			$per_page = 10;
			$data['title'] = "Tìm kiếm danh mục sản phẩm cấp 1";
			$data['table'] = 'category_product';
			$data['controller'] = 'category_product_1';
			$data['list_menu'] = $this->mod_lib->findOrderList($data['table']);
			$data['content_admin'] = 'back_end/category_product_1/index';
			$data['sort'] = 'ASC';
			
			$this->db->order_by('parent_id','asc');
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai	
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
			
			if(!empty($check_search)){
				$_SESSION['like_category_product_1'] = array(
					'name'  => $this->input->POST('nameSearch'),
					'level' => 1
				);																												
			} else {
				$_SESSION['like_category_product_1'] = array(
					'level' => 1
				);
			}
			
			$data_search = $this->mod_lib->search('category_product','',$_SESSION['like_category_product_1'],array(),array("id"=>"desc"));
			$data['admin_category_product_1'] = $this->mod_lib->load_search('category_product','',$_SESSION['like_category_product_1'],array(),$per_page,$this->uri->segment(4),array("order"=>"asc"));		
			$config['base_url']     =  site_url('back_end/category_product/search/');
			$config['total_rows']   =  count($data_search);
			$config['per_page']     =  $per_page;
			$config['uri_segment']  =  4; 
			$this->pagination->initialize($config);		
			$this->load->view('back_end/template_backend',$data);	
		}
		
		function order() {
			//lay ra cac danh muc
			$this->db->where('level',1);
			$cp = $this->db->get('category_product')->result_array();
			$order_admin_menu = $this->input->post('order_category_product_1');
			
			//dem so danh muc 
			$count_order = count($cp);
			$i = 0;
				
			foreach ($order_admin_menu as $order_admin_menu_key => $order_admin_menu_value) {
				//admin menu can thay doi order 
				$this->db->select('id,order');
				$data['order_adm'] = $this->db->get_where('category_product',array('id' => $order_admin_menu_key))->row_array();
				
				//admin_menu trung voi gia tri can thay doi
				$this->db->select('id,order');
				$data['order_adm_value'] = $this->db->get_where('category_product',array('order' => $order_admin_menu_value))->row_array();
					
				//hoa doi gia tri order
				if (!empty($data['order_adm_value'])) {
					if ($data['order_adm']['id'] !== $data['order_adm_value']['id']) {
						$where_1 = array(
			  				'id' => $data['order_adm']['id']
			  			);
			  			
			  			$order_1 = array(
			  				'id' => 'DESC' 
			  			);
			  	  
						$data_value_1 = array (
							'order' => $data['order_adm_value']['order'],
						);	
						
						$this->mod_lib->update('category_product',$where_1,$order_1,$data_value_1);
						
						$where_2 = array(
			  				'id' => $data['order_adm_value']['id']
			  			);
			  			
			  			$order_2 = array(
			  				'id' => 'DESC' 
			  			);
			  	  		
						$data_value_2 = array (
							'order' => $data['order_adm']['order'],
						);	
						
						$this->mod_lib->update('category_product',$where_2,$order_2,$data_value_2);
						
						$this->session->set_flashdata('msview', 'Thay đổi thứ tự thành công !');
					} 		
				}
			}
			
			redirect('back_end/category_product_1');
		}	
		
		function add(){
			$data['title']			= "Thêm danh mục sản phẩm cấp 1";
			$data['content_admin'] = 'back_end/category_product_1/add';
			$data['table'] = 'category_product_1';
			//lay ra so order
			$this->db->where('level',1); 
			$cp_1 = $this->db->get('category_product')->result_array();
			$add = $this->input->post('addform');
			
			$this->db->order_by('parent_id','asc');
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai	
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();

			if(!empty($add)){ 	
				$data_value = array (
					'name'       		=> trim($this->input->post('name')),
					'keyword'       	=> trim($this->input->post('name')),
					'parent_id'	        => 0,
					'create_date'		=> date ("Y-m-d H:m:s"),
					'order'             => count($cp_1) + 1,
					'alias'      		=> convertAlias($this->input->post('name')),
					'status'     		=> trim($this->input->post('status')),
					'level'      		=> 1				
				);
						
				if($this->mod_lib->insert('category_product',$data_value)) {
					$id = $this->db->insert_id();
					
					$data  = array(
			  			'root_path' => $id
			  		);
			  		
					$this->mod_lib->update('category_product',$where,$order,$data);
					
					$this->session->set_flashdata('msview', 'Thêm danh mục sản phẩm thành công !');
					$this->mod_lib->insert_log('Add category_product_1',$_SESSION['admin_id']);
				} else {	
					$this->session->set_flashdata('msview', 'Thêm danh mục sản phẩm thất bại !');
					$this->load->view('back_end/template_backend',$data);
				}
							
				redirect('back_end/category_product_1');
							
			} else {
				$this->load->view('back_end/template_backend',$data);
			}
		}
		
		function edit($id = null){
			$data['title']			= "Sửa danh mục sản phẩm cấp 1";
			$data['content_admin'] = 'back_end/category_product_1/edit';
			$data['category_product_1_value'] = $this->db->get_where('category_product',array( 'id' => $id ))->row_array();
			$edit = $this->input->post('editForm');
			
			$this->db->order_by('parent_id','asc');
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai	
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
			
			if(!empty($edit)){
				
				$where = array(
			  			'id' => $id 
			  		);
			  	$order = array(
			  			'id' => 'DESC' 
			  		);
	
				$data_value = array (
						'name'       => trim($this->input->post('name')),
						'alias'      => convertAlias($this->input->post('name')),
						'keyword'    => trim($this->input->post('keyword')),
						'status'     => trim($this->input->post('status')),
						'root_path'  => $id
					);
	
				if($this->mod_lib->update('category_product',$where,$order,$data_value)){
					$this->session->set_flashdata('msview', 'Sửa thành công !');
				} else {
					$this->session->set_flashdata('msview', 'Sửa thất bại !');
				}
				
				redirect('back_end/category_product_1');
			}else {		
				$this->load->view('back_end/template_backend',$data);		
			}
		}
		
		function sort($field,$sort = ''){
			$per_page = 20;
			$uri = 4;
			$data['title'] = "Sắp xếp danh mục cấp 1";
			$data['table'] = 'category_product';
			$data['controller'] = 'category_product_1';
			$data['content_admin'] = 'back_end/category_product_1/index';
			
			$this->db->order_by('parent_id','asc');
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai	
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
			
			$_SESSION['sort'] = $sort;
			$_SESSION['field'] = $field;
			if ($sort == 'DESC') {
				$data['sort'] =  'ASC';
			} else if($sort == 'ASC') {
				$data['sort'] =  'DESC';
			}
			
			$data_search = $this->mod_lib->search('category_product','',array('level' => 1),array(),array("id"=>"desc"));
			$data['admin_category_product_1'] = $this->mod_lib->load_search('category_product','',array('level' => 1),array(),$per_page,$this->uri->segment($uri),array($_SESSION['field']=>$_SESSION['sort']));		
			$config['base_url']     =  site_url('back_end/category_product_1/index');
			
			$config['total_rows']   =  count($data_search);
			$config['per_page']     =  $per_page;
			$config['uri_segment']  =  $uri; 
			$this->pagination->initialize($config);		
			$this->load->view('back_end/template_backend',$data);		
		}
		
		function active_kh($id = NULL) {
			//lay ra ban ghi can active_kh
			$active_kh = $this->db->get_where('category_product',array('id'=>$id))->row_array();

			if ($active_kh['status'] == 0) {
				$where_active = array(
			  			'id' => $id 
			  		);
			  	$order_active = array();
				$data_value_active = array (
					'status' => 1	
				);	
			} else {
				$where_active = array(
			  			'id' => $id 
			  		);
			  	$order_active = array();
				$data_value_active = array (
					'status' => 0	
				);
			}
			
			$this->mod_lib->update('category_product',$where_active ,$order_active ,$data_value_active);
			
			$this->session->set_flashdata('msview', 'Thay đổi trạng thái thành công !');
			redirect('back_end/category_product_1');	
		}
		
		function active_view($id = NULL) {
			//lay ra ban ghi can active_kh
			$active_kh = $this->db->get_where('category_product',array('id'=>$id))->row_array();
			
			if ($active_kh['active'] == 0) {
				$where_active = array(
			  			'id' => $id 
			  		);
			  	$order_active = array();
				$data_value_active = array (
					'active' => 1	
				);	
			} else {
				$where_active = array(
			  			'id' => $id 
			  		);
			  	$order_active = array();
				$data_value_active = array (
					'active' => 0	
				);
			}
			
			$this->mod_lib->update('category_product',$where_active ,$order_active ,$data_value_active);
				
			//lay ra cac danh muc da kich hoat
			$count_active = $this->db->get_where('category_product',array('active'=>1))->result_array();
			
			if (count($count_active) > 3) {
				$where_active = array(
			  			'id' => $id 
			  		);
			  	$order_active = array();
				$data_value_active = array (
					'active' => 0	
				);	
				
				$this->mod_lib->update('category_product',$where_active ,$order_active ,$data_value_active);
				
				$this->session->set_flashdata('msview', 'Chỉ được hiển thị 3 danh mục sản phẩm trên trang chủ !');
			}
			
			redirect('back_end/category_product_1');	
		}
		
		function delete($id) {
			if(!empty($id)){
				//lay ra order cua ban ghi bi xoa
				$this->db->select('order');
				$this->db->where('level',1);
				$admin_menu_one = $this->db->get_where('category_product',array('id' => $id))->row_array();
				$order_one = $admin_menu_one['order'];
				
				//xoa admin menu
				$this->db->delete('category_product', array('id' => $id));
				
				//lay ra cac ban ghi co gia tri order lon hon gia tri order cua ban ghi da xoa
				$this->db->select('id,order');
				$this->db->where('level',1);
				$this->db->where("order > $order_one");
				$this->db->order_by("order", "asc");
				$category_product_arr = $this->db->get('category_product')->result_array();
				
				//cong them moi gia order 1 don vi
				foreach ($category_product_arr as $category_product_arr_value) {
					$where = array(
				  			'id' => $category_product_arr_value['id']
				  		);		  		
				  	$order = array();
					$data_value = array (
						'order'       => $category_product_arr_value['order'] - 1
					);
					
					$this->mod_lib->update('category_product',$where,$order,$data_value);
				}
				
				//xoa cac san pham di kem
				$id_category = array (
					'category_product_id' => $id
				);
				$this->db->delete('product',$id_category);
				
				$this->session->set_flashdata('msview', 'Xóa thành công !');
			} else {
				$this->session->set_flashdata('msview', 'Xóa thất bại !');
			}		
			
			redirect('back_end/category_product_1');
		}

		
	}
?>